package com.pocketmenu.ui;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.pocketmenu.business.Constants;
import com.pocketmenu.persistence.UserProfile;
import com.pocketmenu.persistence.UserProfileDataSource;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class NewUserActivity extends Activity {

	private UserProfile registrado;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_user);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_new_user, menu);
        return true;
    }
    
    public void bNewUser_Click(View v)
    {
    	UserProfileDataSource datasourceProfile = new UserProfileDataSource(this);
    	
    	datasourceProfile.open();
    	View rlFooter = (RelativeLayout) v.getParent().getParent();    	    	
    	
    	//Profile
    	EditText etNameTemp = (EditText) rlFooter.findViewById(R.id.etName);
    	EditText etEmailTemp = (EditText) rlFooter.findViewById(R.id.etEmail);
    	EditText etAgeTemp = (EditText) rlFooter.findViewById(R.id.etAge);
    	EditText etAddressTemp = (EditText) rlFooter.findViewById(R.id.etAddress);
    	EditText etPasswordTemp = (EditText) rlFooter.findViewById(R.id.etPassword);
    	
    	RadioButton rbSexM = (RadioButton) rlFooter.findViewById(R.id.rbSexoM);
    	RadioButton rbSexF = (RadioButton) rlFooter.findViewById(R.id.rbSexoM);
    	
    	String sexo = rbSexM.isChecked() ? rbSexM.getText().toString() : rbSexF.getText().toString();
    	
    	registrado=datasourceProfile.create(etNameTemp.getText().toString()
    			, etEmailTemp.getText().toString()
    			, etPasswordTemp.getText().toString()
    			, etAgeTemp.getText().toString()
    			, etAddressTemp.getText().toString()
    			, sexo);
    	
    	datasourceProfile.close();
    	Thread networkThread = new Thread() {
       	    @Override
       	    public void run() {
       	      
   	    	//request       	    
       	    String metodo = "agregarUsuario";	
           SoapObject request= new SoapObject(Constants.namespace, metodo);
           request.addProperty("UserProfileAddress", registrado.getAddress());
           request.addProperty("Age", registrado.getAge());
           request.addProperty("Email", registrado.getEmail());
           request.addProperty("UserProfilePassword", registrado.getPassword());
           request.addProperty("Sex", registrado.getSex());
           request.addProperty("Name", registrado.getName());

           //modelo sobre
           SoapSerializationEnvelope sobre= new SoapSerializationEnvelope(SoapEnvelope.VER11);

           sobre.dotNet = true;

           sobre.setOutputSoapObject(request);

           //transporte
           HttpTransportSE transporte= new HttpTransportSE(Constants.url,2000);
           
           transporte.debug = true;           

           //llamada

           try {
    			transporte.call(Constants.namespace+metodo, sobre);
    			
    			//resultado
    			SoapPrimitive resultado= (SoapPrimitive) sobre.getResponse();
    			String nombre=resultado.toString();
    			
    			
    			Log.i("system.out", nombre);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
           catch (XmlPullParserException e) {
    			// TODO Auto-generated catch block
        	   Log.i("system.ou2t",transporte.responseDump);
    		}
                   
       	      }
       	      
       	    };
       	    networkThread.start();
       	 Intent intent = new Intent(this, TabLayoutActivity.class);		      			
 	    startActivity(intent);
    }
    
    public void bCancelNewUser_Click(View view)
    {
    	this.finish();
    }
}
